package com.lzy.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类型验证器
 * @date 2021/3/25 10:28
 * @author lcc
 */
public class TypeValidator implements ConstraintValidator<TypeConstraint, Integer> {

	private TypeConstraint annotation;

	@Override
	public boolean isValid(Integer value, ConstraintValidatorContext context) {
		if(value == null){
			return false;
		}
		int[] values = annotation.values();
		List<Integer> list = Arrays.stream(values).boxed().collect(Collectors.toList());
		return list.contains(value);
	}

	@Override
	public void initialize(TypeConstraint typeConstraintAnnotation) {
		this.annotation = typeConstraintAnnotation;
	}
}
